package com.ls.fw.data.search.impl.lucene.bean;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.lucene.search.ScoreDoc;

/**
 * 请求session,通过条件的hashcode区别不同session
 * 
 * @author ls
 * 
 */
public class Session implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6463510119080455016L;
	
	private String conditionHashCode = "";
	
	//分页信息<page,>
	private Map<String, LastPageInfo> pageInfo = new ConcurrentHashMap<String, LastPageInfo>();

	private long time = System.currentTimeMillis();

	private long total = 0;
	
	public Session() {
		super();
	}

	public Session(String conditionHashCode) {
		super();
		this.conditionHashCode = conditionHashCode;
	}

	
	/**
	 * 获取该页数的分页信息
	 * @param page
	 * @return
	 */
	public LastPageInfo getPageInfo(int page) {
		String pa = String.valueOf(page);
		for (String p : pageInfo.keySet()) {
			if (p.equals(pa)) {
				return pageInfo.get(p);
			}
		}
		return null;
	}

	
	
	/**
	 * 获取该页数的最后doc
	 * @param page
	 * @return
	 */
	public ScoreDoc getDoc(int page) {
		String pa = String.valueOf(page);
		for (String p : pageInfo.keySet()) {
			if (p.equals(pa)) {
				return pageInfo.get(p).getLastDoc();
			}
		}
		return null;
	}
	
	
	/**
	 * 获取该目录的最后分页页数
	 * @param page
	 * @param dir
	 * @return
	 */
	public int getLastPageFromDir(int page,String dir) {
		int n = 0;
		String pa = String.valueOf(page);
		for (String p : pageInfo.keySet()) {
			if (p.equals(pa)) {
				LastPageInfo info = pageInfo.get(p);
				if(info != null){
					if(info.getDir().equals(dir)){
						n = info.getPage()-info.getCurrentDirPage();
						if(n <= 0){
							if(info.getCurrentDirUsedSize()>0){
								n++;
							}
						}
					}
				}
			}
		}
		return n;
	}

	public int getUsedSizeFromDir(int page,String dir) {
		int n = 0;
		String pa = String.valueOf(page);
		for (String p : pageInfo.keySet()) {
			if (p.equals(pa)) {
				LastPageInfo info = pageInfo.get(p);
				if(info != null){
					if(info.getDir().equals(dir)){
						n = info.getCurrentDirUsedSize();
					}
				}
			}
		}
		return n;
	}

	/**
	 * 添加分页信息
	 * @param page
	 * @param info
	 * @return
	 */
	public LastPageInfo put(int page, LastPageInfo info) {
		pageInfo.put(page + "", info);
		return info;
	}
	
	
	/**
	 * 添加分页信息
	 * @param page
	 * @param lastDoc
	 * @param dir
	 * @param currentPage
	 * @return
	 */
	public LastPageInfo put(int page, ScoreDoc lastDoc, String dir,int currentPage) {
		String pa = String.valueOf(page);
		LastPageInfo p = pageInfo.get(pa);
		p.setLastDoc(lastDoc);
		p.setPage(currentPage);
		p.setDir(dir);
		pageInfo.put(pa, p);
		return p;
	}

	@Override
	public String toString() {
		return "Session [conditionHashCode=" + conditionHashCode
				+ ", pageInfo=" + pageInfo + ", time=" + time + "]";
	}

	public void clear() {
		this.pageInfo.clear();
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public void refush() {
		time = System.currentTimeMillis();
	}

	public void delete(String dir) {
		for (String p : pageInfo.keySet()) {
			LastPageInfo info = pageInfo.get(p);
			if (info != null && info.getDir().equals(dir)) {
				pageInfo.remove(p);
				System.out.println("删除" + dir);
				break;
			}
		}
	}

	public String getConditionHashCode() {
		return conditionHashCode;
	}

	public void setConditionHashCode(String conditionHashCode) {
		this.conditionHashCode = conditionHashCode;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((conditionHashCode == null) ? 0 : conditionHashCode
						.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Session other = (Session) obj;
		if (conditionHashCode == null) {
			if (other.conditionHashCode != null)
				return false;
		} else if (!conditionHashCode.equals(other.conditionHashCode))
			return false;
		return true;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

}
